<?php

namespace app\goods\controller;

use think\admin\Controller;
use think\facade\Request;
use think\admin\extend\DataExtend;
use think\admin\helper\QueryHelper;
use app\goods\model\ImsEweiShopGoods;
use app\merch\model\ImsEweiShopMerchUser;
use app\goods\model\ImsEweiShopCategory;

class Index extends Controller
{
    public function index()
    {
        //0待审核1出售中2仓库中-1回收站
        $this->type = $this->get['type'] ?? '1';
        $this->opttype = $this->get['opttype'] ?? '0';
        //获取供货商列表
        $this->merchList = ImsEweiShopMerchUser::mk()->where(['status'=>1])->select();
        $this->tagList = ['isnew'=>'新品','isrecommand'=>'推荐','ishot'=>'热销','isdiscount'=>'折扣'];
        ImsEweiShopGoods::mQuery()->layTable(function () {
            $this->title = '商品列表';
        }, function (QueryHelper $query) {
            // 加载对应数据列表
			$query->alias('g')->leftJoin('ims_ewei_shop_merch_user mu','mu.id = g.merchid');

			$query->field("g.*,mu.merchname");

            $query->where(['g.uniacid' => 2]);
            switch ($this->type) {
                case '1':
                    $query->where(' g.status=1 ');
                    break;
                case '2':
                    $query->where(' g.status=2 ');
                    break;
                case '0':
                    $query->where(' g.status=0 ');
                    break;
                case '-1':
                    $query->where(' g.status=-1 ');
                    break;
                default:
                    $query->where(' g.status not in (0,1,2) ');
                    break;
            }
            switch ($this->opttype) {
                case 'isnew':
                    $query->where(' g.isnew=1 ');
                    break;
                case 'isrecommand':
                    $query->where(' g.isrecommand=1 ');
                    break;
                case 'ishot':
                    $query->where(' g.ishot=1 ');
                    break;
                case 'isdiscount':
                    $query->where(' g.isdiscount=1 ');
                    break;
                default:
                    break;
            }
            // 数据列表搜索过滤
            $query->like('mu.merchname|g.title|g.subtitle|g.keywords#keywords');

            $merchid = Request::param('merchid');
            if ($merchid>0) {
                $query->where('g.merchid','=',$merchid);
            }
        });
        $this->fetch();
    }

    public function add()
    {
        ImsEweiShopGoods::mForm('form');
    }

    public function edit()
    {
        ImsEweiShopGoods::mForm('form');
    }


	/**
	* 复制
	* @auth du
	**/
	public function edit_copy(){
		$id = Request::param('id');		
		$data = ImsEweiShopGoods::mk()->where('id',$id)->select()->toarray();		
		unset($data[0]['id']);
		unset($data[0]['createtime']);		
		$data[0]['status'] = '2'; //仓库中
		
		ImsEweiShopGoods::mk()->create($data[0]);

		//ImsEweiShopGoods::mForm('form');
	}

	/**
	* 参加优惠
	@auth du
	**/
	public function isAct(){		
		 ImsEweiShopGoods::mSave($this->_vali([
            'isdiscount.in:0,1'  => '状态值范围异常！',
            'isdiscount.require' => '状态值不能为空！',
        ]));
	}




    /**
     * 删除
     * @auth true
     */
    public function remove()
    {
        $ids_srt = Request::param('id');
        $ids_arr = array_unique(array_filter(explode(',', $ids_srt)));
        ImsEweiShopGoods::mk()->where('id','in',$ids_arr)->delete();
        ImsEweiShopGoods::mDelete();
    }

     /**
     * 修改状态
     * @auth true
     */
    public function status()
    {
        ImsEweiShopGoods::mSave($this->_vali([
            'status.in:0,1,2,-1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    ### 当一个控制器存在多个page操作时，可以指定回调前缀
    protected function _index_page_filter(&$data){
      // 精准回调对 $data 进行二次处理，注意是引用
        foreach ($data as $k => &$v) {
            $v['thumb'] = get_image_full_url($v['thumb']);
            $tag_arr = [];
            if (isset($v['isnew']) && ($v['isnew'] == 'on' || $v['isnew'] == '1')) {
                $tag_arr[] = '新品';
            }

            if (isset($v['isrecommand']) && ($v['isrecommand'] == 'on' || $v['isrecommand'] == '1')) {
                $tag_arr[] = '推荐';
            }

            if (isset($v['ishot']) && ($v['ishot'] == 'on' || $v['ishot'] == '1')) {
                $tag_arr[] = '热销';
            }

            if (isset($v['isdiscount']) && ($v['isdiscount'] == 'on' || $v['isdiscount'] == '1')) {
                $tag_arr[] = '折扣';
            }
            if (!empty($tag_arr)) {
                $show_str = implode(' | ', $tag_arr);
            }else{
                $show_str = '';
            }
            $v['tag_str'] = $show_str;
        }
    }

    /**
     * 表单数据处理
     * @param array $vo
     */
    protected function _form_filter(array &$vo)
    {

        if ($this->request->isGet()) {
            if (isset($vo['thumb'])) {
                $vo['thumb'] = get_image_full_url($vo['thumb']);
            }
            if (isset($vo['thumb_url'])) {
                $thumb_url_arr = unserialize($vo['thumb_url']);
                if (!empty($thumb_url_arr)) {
                    foreach ($thumb_url_arr as $k => $v) {
                        $thumb_url_arr[$k] = get_image_full_url($v);
                    }
                    $vo['thumb_url'] = implode('|', $thumb_url_arr);
                }else{
                    $vo['thumb_url'] = $vo['thumb'];
                }
            }

            if (isset($vo['isnew']) && ($vo['isnew'] == 'on' || $vo['isnew'] == '1')) {
                $vo['isnew'] = 1;
            }else{
                $vo['isnew'] = 0;
            }

            if (isset($vo['isrecommand']) && ($vo['isrecommand'] == 'on' || $vo['isrecommand'] == '1')) {
                $vo['isrecommand'] = 1;
            }else{
                $vo['isrecommand'] = 0;
            }

            if (isset($vo['ishot']) && ($vo['ishot'] == 'on' || $vo['ishot'] == '1')) {
                $vo['ishot'] = 1;
            }else{
                $vo['ishot'] = 0;
            }

            if (isset($vo['isdiscount']) && ($vo['isdiscount'] == 'on' || $vo['isdiscount'] == '1')) {
                $vo['isdiscount'] = 1;
            }else{
                $vo['isdiscount'] = 0;
            }

            /* 列出可选上级分类 */
            $categorys = ImsEweiShopCategory::mk()->where('status','=',1)->order('sort desc,id asc')->column('id,name,pid,thumb,sort,status,isrecommand', 'id');
            $this->categorys = DataExtend::arr2table($categorys);

            if (isset($vo['pcate']) && $vo['pcate'] == 0) {
                $vo['category'] = 0;
            }elseif (isset($vo['ccate']) && $vo['ccate'] == 0) {
                $vo['category'] = $vo['pcate'];
            }elseif (isset($vo['tcate']) && $vo['tcate'] == 0) {
                $vo['category'] = $vo['ccate'];
            }elseif (isset($vo['tcate']) && $vo['tcate'] != 0) {
                $vo['category'] = $vo['tcate'];
            }
            //获取供货商列表
            $this->merchList = ImsEweiShopMerchUser::mk()->where(['status'=>1])->select();
            $this->goodsStatusList = ImsEweiShopGoods::getGoodsStatusList();
        }elseif ($this->request->isPost()) {
			//if(Request::param('edit_c')){
			//	unset($vo['id']);
		//	}
            $thumb_url_srt = Request::param('thumb_url');
            $thumb_url_arr = array_unique(array_filter(explode('|', $thumb_url_srt)));
            if (isset($thumb_url_arr[0])) {
                $vo['thumb'] = $thumb_url_arr[0];
                $vo['thumb_url'] = serialize($thumb_url_arr);
            }else{
                $vo['thumb'] = '';
                $vo['thumb_url'] = '';
            }

            if (isset($vo['isnew']) && ($vo['isnew'] == 'on' || $vo['isnew'] == '1')) {
                $vo['isnew'] = 1;
            }else{
                $vo['isnew'] = 0;
            }

            if (isset($vo['isrecommand']) && ($vo['isrecommand'] == 'on' || $vo['isrecommand'] == '1')) {
                $vo['isrecommand'] = 1;
            }else{
                $vo['isrecommand'] = 0;
            }

            if (isset($vo['ishot']) && ($vo['ishot'] == 'on' || $vo['ishot'] == '1')) {
                $vo['ishot'] = 1;
            }else{
                $vo['ishot'] = 0;
            }

            if (isset($vo['isdiscount']) && ($vo['isdiscount'] == 'on' || $vo['isdiscount'] == '1')) {
                $vo['isdiscount'] = 1;
            }else{
                $vo['isdiscount'] = 0;
            }

            $fcinfo = ImsEweiShopCategory::mk()->where(['id'=>$vo['category']])->find();
            $pcate = 0;
            $ccate = 0;
            $tcate = 0;
            if ($fcinfo->level == 3) {
                $scinfo = ImsEweiShopCategory::mk()->where(['id'=>$fcinfo->pid])->find();
                $tcinfo = ImsEweiShopCategory::mk()->where(['id'=>$scinfo->pid])->find();
                $pcate = $tcinfo->id;
                $ccate = $scinfo->id;
                $tcate = $fcinfo->id;
            }elseif ($fcinfo->level == 2) {
                $scinfo = ImsEweiShopCategory::mk()->where(['id'=>$fcinfo->pid])->find();
                $pcate = $scinfo->id;
                $ccate = $fcinfo->id;
            }elseif ($fcinfo->level == 1) {
                $pcate = $fcinfo->id;
            }else{

                if ($fcinfo->pid == 0) {
                    $pcate = $fcinfo->id;
                }else{
                    $scinfo = ImsEweiShopCategory::mk()->where(['id'=>$fcinfo->pid])->find();
                    if ($scinfo->pid == 0) {
                        $pcate = $scinfo->id;
                        $ccate = $fcinfo->id;
                    }else{
                        $tcinfo = ImsEweiShopCategory::mk()->where(['id'=>$scinfo->pid])->find();
                        if ($tcinfo->pid == 0) {
                            $pcate = $tcinfo->id;
                            $ccate = $scinfo->id;
                            $tcate = $fcinfo->id;
                        }else{
                            error_log('ImsEweiShopCategory Error');
                        }
                    }
                }
            }

            $vo['pcate'] = $pcate;
            $vo['ccate'] = $ccate;
            $vo['tcate'] = $tcate;
            unset($vo['category']);
            //售价
            if (isset($vo['service_fee_type']) && $vo['service_fee_type']==1) {
                $vo['marketprice'] = $vo['costprice']*$vo['costnum'] + $vo['service_fee_per'];
            }elseif (isset($vo['service_fee_type']) && $vo['service_fee_type']==2) {
                $vo['marketprice'] = ($vo['costprice']*$vo['costnum']) + ($vo['costprice']*$vo['costnum']*$vo['service_fee_per']/100);
            }
        }
	
    }

	
}
